package com.bytenebula.asteroid.config;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {

    @Primary
    @Bean(name = "bytenebulaDataSource")
    @Qualifier("bytenebulaDataSource")
    @ConfigurationProperties(prefix="mybatis-flex.datasource.bytenebula")
    public HikariDataSource bytenebulaDataSource() {
        return DataSourceBuilder.create().type(HikariDataSource.class).build();
    }

    @Bean(name = "warehouseDataSource")
    @Qualifier("warehouseDataSource")
    @ConfigurationProperties(prefix="mybatis-flex.datasource.warehouse")
    public HikariDataSource warehouseDataSource() {
        return DataSourceBuilder.create().type(HikariDataSource.class).build();
    }

    @Bean(name = "warehouseJdbcTemplate")
    public JdbcTemplate warehouseJdbcTemplate(
            @Qualifier("warehouseDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}
